package im.zhaojun.system.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import im.zhaojun.common.util.JsonResult;
import im.zhaojun.system.model.PUpdate;
import im.zhaojun.system.service.PUpdateService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/pu")
public class PUpdateController extends BaseController{
@Autowired
    PUpdateService pUpdateService;


    @GetMapping("/pupdate")
    public String index() {
        return "xinxi/xinxi-list";
    }

    @GetMapping("/editDetails")
    public String editDetails() {
        return "xinxi/xinxi-editDetails";
    }
    @GetMapping("/add")
    public String add() {
        return "xinxi/xinxi-add";
    }


    /*表格数据查询展示*/
    @RequestMapping("/SelectAllsPUpdate")
    @ResponseBody
    public JsonResult<List<PUpdate>> selectAlls(@RequestParam(value = "page", defaultValue = "1") int page,
                                                @RequestParam(value = "limit", defaultValue = "10")int limit,
                                                String variable,String groups,
                                                String levels
                                                ){
        System.err.println("SelectAllsPUpdate查询信息");
        JsonResult result = new JsonResult();
        System.err.println(variable);
        System.err.println(groups);
        System.err.println(levels);
        try {
            PageHelper.startPage(page, limit);
            List<PUpdate> data =  pUpdateService.SelectAllsPUpdate(variable,groups,levels);
            PageInfo<PUpdate> pageInfo = new PageInfo<>(data);
            Long total = pageInfo.getTotal();

            String msg = "0";

            return new JsonResult<>(SUCCESS, data,total.intValue(), msg);
        }catch (Exception e){
            result.setMessage("出现异常，请重新尝试");
        }


        return result;
    }



    /*根据id查询数据回显*/
    @RequestMapping("/selectById")
    @ResponseBody
    public JsonResult<PUpdate> selectById(Integer id){
        System.err.println("根据id查询数据回显编辑");
        JsonResult result = new JsonResult();

        try {
            PUpdate data =  pUpdateService.selectById(id);
            String levels = data.getLevels();
            System.out.println(levels);
            if(levels.equals("1")){
                data.setLevels("紧急报警");
                return new JsonResult<>(SUCCESS, data);
            }
            if(levels.equals("2")){
                data.setLevels("重要报警");
                return new JsonResult<>(SUCCESS, data);
            }
            if(levels.equals("3")){
                data.setLevels("一般报警");
                return new JsonResult<>(SUCCESS, data);
            }

        }catch (Exception e){
            result.setMessage("出现异常，请重新尝试");
        }

        return result;
    }

    /*根据id修改回显数据*/
    @RequestMapping("/updateById")
    @ResponseBody
    public JsonResult<PUpdate> updateById(@Param("id") Integer id,@Param("variable") String variable,@Param("groups") String groups
            ,@Param("levels") String levels,@Param("recovery") String recovery,@Param("plimit") String plimit,@Param("describes") String describes
                                          ){
        System.err.println("根据id修改回显数据");
        JsonResult result = new JsonResult();


        try {
            if(levels.equals("紧急报警")){
                levels = "1";
            }
            if(levels.equals("重要报警")){
                levels = "2";
            }
            if(levels.equals("一般报警")){
                levels = "3";
            }
            System.out.println("levels==  "+levels);
            pUpdateService.updateById(id,variable,groups,levels,recovery,plimit,describes);
            result.setState(SUCCESS);

        }catch (Exception e){
            result.setMessage("出现异常，请重新尝试");
        }

        return result;
    }

    /*根据id删除数据*/
    @RequestMapping("/deleteById")
    @ResponseBody
    public JsonResult<PUpdate> deleteById(@Param("id") Integer id ){
        System.err.println("根据id删除数据");
        JsonResult result = new JsonResult();

        try {
            pUpdateService.deleteById(id);
            result.setState(SUCCESS);

        }catch (Exception e){
            result.setMessage("出现异常，请重新尝试");
        }

        return result;
    }



    /*添加新数据*/
    @RequestMapping("/insertPUpdate")
    @ResponseBody
    public JsonResult<PUpdate> insertPUpdate(PUpdate pUpdate){
        System.err.println("添加新数据");
        JsonResult result = new JsonResult();
        System.out.println(pUpdate);
        try {
            pUpdateService.insertPUpdate(pUpdate);
            result.setState(SUCCESS);

        }catch (Exception e){
            result.setMessage("出现异常，请重新尝试");
        }

        return result;
    }



}
